package ru.averta.notes.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ru.averta.notes.domain.Site;
import ru.averta.notes.service.search.SubjectCriteriaSimpleBean;
import ru.averta.notes.service.search.SubjectCriteriaSearchService;
import ru.averta.notes.service.security.SecurityService;

import java.util.List;
import java.util.Map;

/**
 * <code>MainDisplayController</code>
 *
 * Основной контроллер, который реализует сложную логику фильтрации subject'ов
 *
 * @author: fedor.belov (fedor.belov@mail.ru)
 * Date: 24.05.11 23:39
 */

@Controller
public class MainDisplayController {

    private final Logger logger = LoggerFactory.getLogger(MainDisplayController.class);

    @Autowired SubjectCriteriaSearchService criteriaSearch;
    @Autowired SecurityService securityService;

    @RequestMapping(value = "/notes/list/{target}/**", method = RequestMethod.GET)
    public String listDisplay(
            @PathVariable String target,
            @RequestParam(required = false) String tags,
            @RequestParam(required = false) String types,
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) Byte favorite,
            Map model) {

        List<Site> files = getSites(target, tags, types, id, favorite);

        logger.debug("У текущего пользователя нашлось заметок - {}", files.size());

        model.put("files", files);

        return "mainListPage.fedor";
    }

    private List<Site> getSites(String target, String tags, String types, Long id, Byte favorite) {

        SubjectCriteriaSimpleBean criteria = getCriteriaForParams(
                target, tags, types, id, favorite);
        return criteriaSearch.getCriteriaResult(criteria);
    }

    private static String[] convertRequestParam(String source) {
        if (source == null) {
            return null;
        } else {
            return source.substring(1, source.length() - 1).split(",");
        }
    }

    public static SubjectCriteriaSimpleBean getCriteriaForParams(String target, String tags, String types, Long id, Byte favorite) {

        SubjectCriteriaSimpleBean criteria = new SubjectCriteriaSimpleBean();

        //Выбираем цель
        if (target.equals("all")) {
            criteria.setTargetAll();
        } else if (target.equals("inbox")) {
            criteria.setTargetInbox();
        } else if (target.equals(("archive"))) {
            criteria.setTargetArchive();
        } else {
            criteria.setTargetInbox(); //По-умолчанию
        }

        criteria.filterByTags(convertRequestParam(tags));
        if (favorite != null && favorite == 1) criteria.filterByFavorite();

        return criteria;
    }
}
